using System;
using System.Linq;

using KTOP.Core.Domain.Matrix;

using KTOP.Homeworks.Hw5.Logics.HamiltonianRoutes;

namespace KTOP.Homeworks.Hw5.Logics
{
    internal class MatrixRebuilder
    {
        public static RMatrix RebuildMatrix(RMatrix source, HamiltonianRoute hamiltonianRoute)
        {
            var eTable = hamiltonianRoute.Route.ToArray();

            if (eTable.Length != source.Size)
                throw new ArgumentException("hamiltonianRoute");

            var rMatrix = Enumerable.Range(0, source.Size)
                .Select(s => new int[source.Size])
                .ToArray();

            for (var row = 0; row < source.Size; row++)
                for (var col = 0; col < source.Size; col++)
                    rMatrix[row][col] = source[eTable[row], eTable[col]];

            return new RMatrix(rMatrix);
        }
    }
}